<template>
	<view>
		<view class="header">
			<image src="../../../../static/icons/arrl.png" mode="" @tap="back"></image>
			<text>乐居</text>
		</view>
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm"></simple-address>
		<view class="list">
			<view class="item">
				<view>收件人</view>
				<view>
					<input v-model="data.name" class="ipt" type="text" placeholder="请输入收件人姓名" />
				</view>
			</view>
			<view class="item">
				<view>电话号码</view>
				<view>
					<input v-model="data.tel" class="ipt" type="text" value="" placeholder="请输入收件人电话号码" />
				</view>
			</view>
			<view class="item">
				<view>所在地区</view>
				<view @tap="open">
					<view class="er" v-if="cityList">{{cityList}}</view>
					<view v-else>请点击选择地址</view>
				</view>
			</view>
			<view class="item">
				<view>详细地址</view>
				<view>
					<textarea v-model="data.detailArea" placeholder="输入详细地址" auto-height />
				</view>
			</view>
			<view class="item">
				<view>设置默认地址</view>
				<view>
					<switch :checked="isChecked" @change="statusEdit" />
				</view>
			</view>
		</view>
		<view v-if="isShow" class="delEdit" @tap="deleteEdit">删除收货地址</view>
		<view class="addSite" @tap="updateSite">保存地址</view>
	</view>
</template>

<script>
	import simpleAddress from '@/components/simple-address/simple-address.vue';
	import { delAddressInfo,switchDefault,updateAddressInfo,addAddressInfo } from "../../../../api/user/index.js"
	export default {
		data() {
			return {
				cityPickerValueDefault:[0, 0, 1],
				cityList:"",
				delId:"",
				isChecked:false,
				statu:0,
				data:{
					name:"",
					tel:"",
					detailArea:""
				},
				list:{},
				isShow:true
			};
		},
		methods:{
			back() {
				uni.navigateTo({
					url:"/pages/user/site/site"
				})
			},
			open() {
				this.cityPickerValueDefault = [0,0,1]
				this.$refs.simpleAddress.open();
			},
			onConfirm(e) {
				console.log(e)
				this.cityList = e.labelArr
				this.cityList = this.cityList.join("-")
			},
			deleteEdit() {
				delAddressInfo(this.delId).then(res=>{
					console.log(res)
					if (res.success) {
						uni.navigateTo({
							url:"/pages/user/site/site"
						})
					}
				})
			},
			statusEdit(e) {
				console.log(e)
				this.isChecked = e.detail.value
				if (this.isChecked) {
					this.statu = 1
				} else {
					this.statu = 0
				}
				switchDefault(this.delId,this.statu).then(res=>{
					console.log(res)
				})
			},
			updateSite() {
				if (this.isShow) {
					updateAddressInfo({
						name:this.data.name,
						phoneNumber:this.data.tel,
						province:this.cityList,
						detailAddress:this.data.detailArea,
						defaultStatus:this.statu,
						id:this.delId
					}).then(res=>{
						console.log("我是更改地址",res)
						console.log("我是状态",this.statu)
						if (res.success) {
							uni.navigateTo({
								url:"/pages/user/site/site"
							})
						}
					})
				} else {
					console.log("我是新增")
					addAddressInfo({
						name:this.data.name,
						phoneNumber:this.data.tel,
						province:this.cityList,
						detailAddress:this.data.detailArea,
						defaultStatus:this.statu
					}).then(res=>{
						console.log(res)
						if (res.success) {
							uni.navigateTo({
								url:"/pages/user/site/site"
							})
						}
					})
				}
			}
		},
		components: {
		        simpleAddress
		 },
		 onLoad(options) {
		 	if (options.id) {
				console.log(options)
				this.delId = options.id
				this.statu = Number(options.status)
				console.log(this.statu)
				if (options.status == "1") {
					this.isChecked = true
				} else {
					this.isChecked = false
				}
				this.list = JSON.parse(options.item)
				console.log("我是list",this.list)
				
				this.data.name = this.list.name
				this.data.tel = this.list.phoneNumber
				this.data.detailArea = this.list.detailAddress
				this.cityList = this.list.province
			} else {
				console.log("我是添加页面")
				this.isShow = false
			}
		 }
	}
</script>

<style lang="scss" scoped>
.header {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #354e44;
	height: 60rpx;
	padding: 14rpx 6rpx;
	image {
		position: absolute;
		left: 4rpx;
		top: 50%;
		transform: translateY(-50%);
		width: 40rpx;
		height: 40rpx;
	}
	text {
		font-size: 32rpx;
		color: #FFFFFF;
		font-weight: bolder;
	}
}
.list {
	.item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 94%;
		height: 120rpx;
		border-bottom: 2rpx solid #ccc;
		margin: 0 3%;
		>view {
			&:nth-child(1) {
				font-size: 30rpx;
			}
			&:nth-child(2) {
				width: 70%;
				.ipt {
					width: 350rpx;
				}
			}
		}
		&:nth-child(3) {
			>view {
				&:nth-child(2) {
					.er {
						display: flex;
					}
				}
			}
		}
		&:last-child {
			>view {
				&:last-child {
					width: 100rpx;
				}
			}
		}
	}
}
.delEdit {
	width: 94%;
	height: 100rpx;
	background-color: #e64340;
	font-size: 36rpx;
	color: #FFFFFF;
	text-align: center;
	line-height: 100rpx;
	margin: 60rpx auto 0;
}
.addSite {
	position: fixed;
	bottom: 20rpx;
	left: 50%;
	transform: translateX(-50%);
	width: 70%;
	height: 80rpx;
	background-color: #354e44;
	border-radius: 80rpx;
	box-shadow: 0px 2px 5px rgba(0,0,0,.4);
	font-size: 30rpx;
	color: #FFFFFF;
	text-align: center;
	line-height: 80rpx;
}
</style>
